{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.1 Wide vs. Narrow Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.2 Creating a Pivot Table from a DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Name</th>\n",
       "      <th>Customer</th>\n",
       "      <th>Revenue</th>\n",
       "      <th>Expenses</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1/1/20</td>\n",
       "      <td>Oscar</td>\n",
       "      <td>Logistics XYZ</td>\n",
       "      <td>5250</td>\n",
       "      <td>531</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1/1/20</td>\n",
       "      <td>Oscar</td>\n",
       "      <td>Money Corp.</td>\n",
       "      <td>4406</td>\n",
       "      <td>661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1/2/20</td>\n",
       "      <td>Oscar</td>\n",
       "      <td>PaperMaven</td>\n",
       "      <td>8661</td>\n",
       "      <td>1401</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1/3/20</td>\n",
       "      <td>Oscar</td>\n",
       "      <td>PaperGenius</td>\n",
       "      <td>7075</td>\n",
       "      <td>906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1/4/20</td>\n",
       "      <td>Oscar</td>\n",
       "      <td>Paper Pound</td>\n",
       "      <td>2524</td>\n",
       "      <td>1767</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Date   Name       Customer  Revenue  Expenses\n",
       "0  1/1/20  Oscar  Logistics XYZ     5250       531\n",
       "1  1/1/20  Oscar    Money Corp.     4406       661\n",
       "2  1/2/20  Oscar     PaperMaven     8661      1401\n",
       "3  1/3/20  Oscar    PaperGenius     7075       906\n",
       "4  1/4/20  Oscar    Paper Pound     2524      1767"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\"sales_by_employee.csv\").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Name</th>\n",
       "      <th>Customer</th>\n",
       "      <th>Revenue</th>\n",
       "      <th>Expenses</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>Creed</td>\n",
       "      <td>Money Corp.</td>\n",
       "      <td>4430</td>\n",
       "      <td>548</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>2020-01-02</td>\n",
       "      <td>Creed</td>\n",
       "      <td>Average Paper Co.</td>\n",
       "      <td>8026</td>\n",
       "      <td>1906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>2020-01-02</td>\n",
       "      <td>Creed</td>\n",
       "      <td>Average Paper Co.</td>\n",
       "      <td>5188</td>\n",
       "      <td>1768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>2020-01-04</td>\n",
       "      <td>Creed</td>\n",
       "      <td>PaperMaven</td>\n",
       "      <td>3144</td>\n",
       "      <td>1314</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>2020-01-05</td>\n",
       "      <td>Creed</td>\n",
       "      <td>Money Corp.</td>\n",
       "      <td>938</td>\n",
       "      <td>1053</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Date   Name           Customer  Revenue  Expenses\n",
       "21 2020-01-01  Creed        Money Corp.     4430       548\n",
       "22 2020-01-02  Creed  Average Paper Co.     8026      1906\n",
       "23 2020-01-02  Creed  Average Paper Co.     5188      1768\n",
       "24 2020-01-04  Creed         PaperMaven     3144      1314\n",
       "25 2020-01-05  Creed        Money Corp.      938      1053"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales = pd.read_csv(\"sales_by_employee.csv\",\n",
    "                    parse_dates = [\"Date\"])\n",
    "\n",
    "sales.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.2.1 The pivot_table Method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Expenses</th>\n",
       "      <th>Revenue</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>637.500000</td>\n",
       "      <td>4293.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>1244.400000</td>\n",
       "      <td>7303.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>1313.666667</td>\n",
       "      <td>4865.833333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>1450.600000</td>\n",
       "      <td>3948.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>1196.250000</td>\n",
       "      <td>4834.750000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               Expenses      Revenue\n",
       "Date                                \n",
       "2020-01-01   637.500000  4293.500000\n",
       "2020-01-02  1244.400000  7303.000000\n",
       "2020-01-03  1313.666667  4865.833333\n",
       "2020-01-04  1450.600000  3948.000000\n",
       "2020-01-05  1196.250000  4834.750000"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(index = \"Date\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Expenses</th>\n",
       "      <th>Revenue</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>637.500000</td>\n",
       "      <td>4293.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>1244.400000</td>\n",
       "      <td>7303.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>1313.666667</td>\n",
       "      <td>4865.833333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>1450.600000</td>\n",
       "      <td>3948.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>1196.250000</td>\n",
       "      <td>4834.750000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               Expenses      Revenue\n",
       "Date                                \n",
       "2020-01-01   637.500000  4293.500000\n",
       "2020-01-02  1244.400000  7303.000000\n",
       "2020-01-03  1313.666667  4865.833333\n",
       "2020-01-04  1450.600000  3948.000000\n",
       "2020-01-05  1196.250000  4834.750000"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(index = \"Date\", aggfunc = \"mean\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Expenses</th>\n",
       "      <th>Revenue</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>3825</td>\n",
       "      <td>25761</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>6222</td>\n",
       "      <td>36515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>7882</td>\n",
       "      <td>29195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>7253</td>\n",
       "      <td>19740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>4785</td>\n",
       "      <td>19339</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Expenses  Revenue\n",
       "Date                         \n",
       "2020-01-01      3825    25761\n",
       "2020-01-02      6222    36515\n",
       "2020-01-03      7882    29195\n",
       "2020-01-04      7253    19740\n",
       "2020-01-05      4785    19339"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(index = \"Date\", aggfunc = \"sum\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Revenue</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>25761</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>36515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>29195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>19740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>19339</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Revenue\n",
       "Date               \n",
       "2020-01-01    25761\n",
       "2020-01-02    36515\n",
       "2020-01-03    29195\n",
       "2020-01-04    19740\n",
       "2020-01-05    19339"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(\n",
    "    index = \"Date\", values = \"Revenue\", aggfunc = \"sum\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Name</th>\n",
       "      <th>Creed</th>\n",
       "      <th>Dwight</th>\n",
       "      <th>Jim</th>\n",
       "      <th>Michael</th>\n",
       "      <th>Oscar</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>4430.0</td>\n",
       "      <td>2639.0</td>\n",
       "      <td>1864.0</td>\n",
       "      <td>7172.0</td>\n",
       "      <td>9656.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>13214.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8278.0</td>\n",
       "      <td>6362.0</td>\n",
       "      <td>8661.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>NaN</td>\n",
       "      <td>11912.0</td>\n",
       "      <td>4226.0</td>\n",
       "      <td>5982.0</td>\n",
       "      <td>7075.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>3144.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6155.0</td>\n",
       "      <td>7917.0</td>\n",
       "      <td>2524.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>938.0</td>\n",
       "      <td>7771.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7837.0</td>\n",
       "      <td>2793.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Name          Creed   Dwight     Jim  Michael   Oscar\n",
       "Date                                                 \n",
       "2020-01-01   4430.0   2639.0  1864.0   7172.0  9656.0\n",
       "2020-01-02  13214.0      NaN  8278.0   6362.0  8661.0\n",
       "2020-01-03      NaN  11912.0  4226.0   5982.0  7075.0\n",
       "2020-01-04   3144.0      NaN  6155.0   7917.0  2524.0\n",
       "2020-01-05    938.0   7771.0     NaN   7837.0  2793.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(\n",
    "    index = \"Date\",\n",
    "    columns = \"Name\",\n",
    "    values = \"Revenue\",\n",
    "    aggfunc = \"sum\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Name</th>\n",
       "      <th>Creed</th>\n",
       "      <th>Dwight</th>\n",
       "      <th>Jim</th>\n",
       "      <th>Michael</th>\n",
       "      <th>Oscar</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>4430</td>\n",
       "      <td>2639</td>\n",
       "      <td>1864</td>\n",
       "      <td>7172</td>\n",
       "      <td>9656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>13214</td>\n",
       "      <td>0</td>\n",
       "      <td>8278</td>\n",
       "      <td>6362</td>\n",
       "      <td>8661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>0</td>\n",
       "      <td>11912</td>\n",
       "      <td>4226</td>\n",
       "      <td>5982</td>\n",
       "      <td>7075</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>3144</td>\n",
       "      <td>0</td>\n",
       "      <td>6155</td>\n",
       "      <td>7917</td>\n",
       "      <td>2524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>938</td>\n",
       "      <td>7771</td>\n",
       "      <td>0</td>\n",
       "      <td>7837</td>\n",
       "      <td>2793</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Name        Creed  Dwight   Jim  Michael  Oscar\n",
       "Date                                           \n",
       "2020-01-01   4430    2639  1864     7172   9656\n",
       "2020-01-02  13214       0  8278     6362   8661\n",
       "2020-01-03      0   11912  4226     5982   7075\n",
       "2020-01-04   3144       0  6155     7917   2524\n",
       "2020-01-05    938    7771     0     7837   2793"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(\n",
    "    index = \"Date\", \n",
    "    columns = \"Name\", \n",
    "    values = \"Revenue\", \n",
    "    aggfunc = \"sum\",\n",
    "    fill_value = 0\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Name</th>\n",
       "      <th>Creed</th>\n",
       "      <th>Dwight</th>\n",
       "      <th>Jim</th>\n",
       "      <th>Michael</th>\n",
       "      <th>Oscar</th>\n",
       "      <th>All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01 00:00:00</th>\n",
       "      <td>4430</td>\n",
       "      <td>2639</td>\n",
       "      <td>1864</td>\n",
       "      <td>7172</td>\n",
       "      <td>9656</td>\n",
       "      <td>25761</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02 00:00:00</th>\n",
       "      <td>13214</td>\n",
       "      <td>0</td>\n",
       "      <td>8278</td>\n",
       "      <td>6362</td>\n",
       "      <td>8661</td>\n",
       "      <td>36515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03 00:00:00</th>\n",
       "      <td>0</td>\n",
       "      <td>11912</td>\n",
       "      <td>4226</td>\n",
       "      <td>5982</td>\n",
       "      <td>7075</td>\n",
       "      <td>29195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04 00:00:00</th>\n",
       "      <td>3144</td>\n",
       "      <td>0</td>\n",
       "      <td>6155</td>\n",
       "      <td>7917</td>\n",
       "      <td>2524</td>\n",
       "      <td>19740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05 00:00:00</th>\n",
       "      <td>938</td>\n",
       "      <td>7771</td>\n",
       "      <td>0</td>\n",
       "      <td>7837</td>\n",
       "      <td>2793</td>\n",
       "      <td>19339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <td>21726</td>\n",
       "      <td>22322</td>\n",
       "      <td>20523</td>\n",
       "      <td>35270</td>\n",
       "      <td>30709</td>\n",
       "      <td>130550</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Name                 Creed  Dwight    Jim  Michael  Oscar     All\n",
       "Date                                                             \n",
       "2020-01-01 00:00:00   4430    2639   1864     7172   9656   25761\n",
       "2020-01-02 00:00:00  13214       0   8278     6362   8661   36515\n",
       "2020-01-03 00:00:00      0   11912   4226     5982   7075   29195\n",
       "2020-01-04 00:00:00   3144       0   6155     7917   2524   19740\n",
       "2020-01-05 00:00:00    938    7771      0     7837   2793   19339\n",
       "All                  21726   22322  20523    35270  30709  130550"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(\n",
    "    index = \"Date\",\n",
    "    columns = \"Name\",\n",
    "    values = \"Revenue\",\n",
    "    aggfunc = \"sum\",\n",
    "    fill_value = 0,\n",
    "    margins = True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Name</th>\n",
       "      <th>Creed</th>\n",
       "      <th>Dwight</th>\n",
       "      <th>Jim</th>\n",
       "      <th>Michael</th>\n",
       "      <th>Oscar</th>\n",
       "      <th>Total</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01 00:00:00</th>\n",
       "      <td>4430</td>\n",
       "      <td>2639</td>\n",
       "      <td>1864</td>\n",
       "      <td>7172</td>\n",
       "      <td>9656</td>\n",
       "      <td>25761</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02 00:00:00</th>\n",
       "      <td>13214</td>\n",
       "      <td>0</td>\n",
       "      <td>8278</td>\n",
       "      <td>6362</td>\n",
       "      <td>8661</td>\n",
       "      <td>36515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03 00:00:00</th>\n",
       "      <td>0</td>\n",
       "      <td>11912</td>\n",
       "      <td>4226</td>\n",
       "      <td>5982</td>\n",
       "      <td>7075</td>\n",
       "      <td>29195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04 00:00:00</th>\n",
       "      <td>3144</td>\n",
       "      <td>0</td>\n",
       "      <td>6155</td>\n",
       "      <td>7917</td>\n",
       "      <td>2524</td>\n",
       "      <td>19740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05 00:00:00</th>\n",
       "      <td>938</td>\n",
       "      <td>7771</td>\n",
       "      <td>0</td>\n",
       "      <td>7837</td>\n",
       "      <td>2793</td>\n",
       "      <td>19339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Total</th>\n",
       "      <td>21726</td>\n",
       "      <td>22322</td>\n",
       "      <td>20523</td>\n",
       "      <td>35270</td>\n",
       "      <td>30709</td>\n",
       "      <td>130550</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Name                 Creed  Dwight    Jim  Michael  Oscar   Total\n",
       "Date                                                             \n",
       "2020-01-01 00:00:00   4430    2639   1864     7172   9656   25761\n",
       "2020-01-02 00:00:00  13214       0   8278     6362   8661   36515\n",
       "2020-01-03 00:00:00      0   11912   4226     5982   7075   29195\n",
       "2020-01-04 00:00:00   3144       0   6155     7917   2524   19740\n",
       "2020-01-05 00:00:00    938    7771      0     7837   2793   19339\n",
       "Total                21726   22322  20523    35270  30709  130550"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(\n",
    "    index = \"Date\", \n",
    "    columns = \"Name\", \n",
    "    values = \"Revenue\", \n",
    "    aggfunc = \"sum\",\n",
    "    fill_value = 0,\n",
    "    margins = True,\n",
    "    margins_name = \"Total\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.2.2 Additional Options for Pivot Tables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Name</th>\n",
       "      <th>Creed</th>\n",
       "      <th>Dwight</th>\n",
       "      <th>Jim</th>\n",
       "      <th>Michael</th>\n",
       "      <th>Oscar</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Name        Creed  Dwight  Jim  Michael  Oscar\n",
       "Date                                          \n",
       "2020-01-01    1.0     1.0  1.0      1.0    2.0\n",
       "2020-01-02    2.0     NaN  1.0      1.0    1.0\n",
       "2020-01-03    NaN     3.0  1.0      1.0    1.0\n",
       "2020-01-04    1.0     NaN  2.0      1.0    1.0\n",
       "2020-01-05    1.0     1.0  NaN      1.0    1.0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(\n",
    "    index = \"Date\", \n",
    "    columns = \"Name\",\n",
    "    values = \"Revenue\",\n",
    "    aggfunc = \"count\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"5\" halign=\"left\">sum</th>\n",
       "      <th colspan=\"5\" halign=\"left\">count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th>Creed</th>\n",
       "      <th>Dwight</th>\n",
       "      <th>Jim</th>\n",
       "      <th>Michael</th>\n",
       "      <th>Oscar</th>\n",
       "      <th>Creed</th>\n",
       "      <th>Dwight</th>\n",
       "      <th>Jim</th>\n",
       "      <th>Michael</th>\n",
       "      <th>Oscar</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>4430</td>\n",
       "      <td>2639</td>\n",
       "      <td>1864</td>\n",
       "      <td>7172</td>\n",
       "      <td>9656</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>13214</td>\n",
       "      <td>0</td>\n",
       "      <td>8278</td>\n",
       "      <td>6362</td>\n",
       "      <td>8661</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>0</td>\n",
       "      <td>11912</td>\n",
       "      <td>4226</td>\n",
       "      <td>5982</td>\n",
       "      <td>7075</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>3144</td>\n",
       "      <td>0</td>\n",
       "      <td>6155</td>\n",
       "      <td>7917</td>\n",
       "      <td>2524</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>938</td>\n",
       "      <td>7771</td>\n",
       "      <td>0</td>\n",
       "      <td>7837</td>\n",
       "      <td>2793</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              sum                            count                         \n",
       "Name        Creed Dwight   Jim Michael Oscar Creed Dwight Jim Michael Oscar\n",
       "Date                                                                       \n",
       "2020-01-01   4430   2639  1864    7172  9656     1      1   1       1     2\n",
       "2020-01-02  13214      0  8278    6362  8661     2      0   1       1     1\n",
       "2020-01-03      0  11912  4226    5982  7075     0      3   1       1     1\n",
       "2020-01-04   3144      0  6155    7917  2524     1      0   2       1     1\n",
       "2020-01-05    938   7771     0    7837  2793     1      1   0       1     1"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(\n",
    "    index = \"Date\",\n",
    "    columns = \"Name\",\n",
    "    values = \"Revenue\",\n",
    "    aggfunc = [\"sum\", \"count\"],\n",
    "    fill_value = 0\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"5\" halign=\"left\">Expenses</th>\n",
       "      <th colspan=\"5\" halign=\"left\">Revenue</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th>Creed</th>\n",
       "      <th>Dwight</th>\n",
       "      <th>Jim</th>\n",
       "      <th>Michael</th>\n",
       "      <th>Oscar</th>\n",
       "      <th>Creed</th>\n",
       "      <th>Dwight</th>\n",
       "      <th>Jim</th>\n",
       "      <th>Michael</th>\n",
       "      <th>Oscar</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>548</td>\n",
       "      <td>368</td>\n",
       "      <td>1305</td>\n",
       "      <td>412</td>\n",
       "      <td>531</td>\n",
       "      <td>4430</td>\n",
       "      <td>2639</td>\n",
       "      <td>1864</td>\n",
       "      <td>7172</td>\n",
       "      <td>4406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>1768</td>\n",
       "      <td>0</td>\n",
       "      <td>462</td>\n",
       "      <td>685</td>\n",
       "      <td>1401</td>\n",
       "      <td>5188</td>\n",
       "      <td>0</td>\n",
       "      <td>8278</td>\n",
       "      <td>6362</td>\n",
       "      <td>8661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>0</td>\n",
       "      <td>758</td>\n",
       "      <td>1923</td>\n",
       "      <td>1772</td>\n",
       "      <td>906</td>\n",
       "      <td>0</td>\n",
       "      <td>2703</td>\n",
       "      <td>4226</td>\n",
       "      <td>5982</td>\n",
       "      <td>7075</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>1314</td>\n",
       "      <td>0</td>\n",
       "      <td>426</td>\n",
       "      <td>1857</td>\n",
       "      <td>1767</td>\n",
       "      <td>3144</td>\n",
       "      <td>0</td>\n",
       "      <td>2287</td>\n",
       "      <td>7917</td>\n",
       "      <td>2524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>1053</td>\n",
       "      <td>1475</td>\n",
       "      <td>0</td>\n",
       "      <td>1633</td>\n",
       "      <td>624</td>\n",
       "      <td>938</td>\n",
       "      <td>7771</td>\n",
       "      <td>0</td>\n",
       "      <td>7837</td>\n",
       "      <td>2793</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Expenses                            Revenue                       \\\n",
       "Name          Creed Dwight   Jim Michael Oscar   Creed Dwight   Jim Michael   \n",
       "Date                                                                          \n",
       "2020-01-01      548    368  1305     412   531    4430   2639  1864    7172   \n",
       "2020-01-02     1768      0   462     685  1401    5188      0  8278    6362   \n",
       "2020-01-03        0    758  1923    1772   906       0   2703  4226    5982   \n",
       "2020-01-04     1314      0   426    1857  1767    3144      0  2287    7917   \n",
       "2020-01-05     1053   1475     0    1633   624     938   7771     0    7837   \n",
       "\n",
       "                  \n",
       "Name       Oscar  \n",
       "Date              \n",
       "2020-01-01  4406  \n",
       "2020-01-02  8661  \n",
       "2020-01-03  7075  \n",
       "2020-01-04  2524  \n",
       "2020-01-05  2793  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(\n",
    "    index = \"Date\",\n",
    "    columns = \"Name\", \n",
    "    values = [\"Revenue\", \"Expenses\"],\n",
    "    fill_value = 0,\n",
    "    aggfunc = { \"Revenue\": \"min\", \"Expenses\": \"min\" }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Revenue</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Creed</th>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>4430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>13214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>3144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Dwight</th>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>2639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>11912</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>7771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Jim</th>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>1864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>8278</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>4226</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   Revenue\n",
       "Name   Date               \n",
       "Creed  2020-01-01     4430\n",
       "       2020-01-02    13214\n",
       "       2020-01-04     3144\n",
       "       2020-01-05      938\n",
       "Dwight 2020-01-01     2639\n",
       "       2020-01-03    11912\n",
       "       2020-01-05     7771\n",
       "Jim    2020-01-01     1864\n",
       "       2020-01-02     8278\n",
       "       2020-01-03     4226"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(\n",
    "    index = [\"Name\", \"Date\"], values = \"Revenue\", aggfunc = \"sum\"\n",
    ").head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Revenue</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2020-01-01</th>\n",
       "      <th>Creed</th>\n",
       "      <td>4430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dwight</th>\n",
       "      <td>2639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jim</th>\n",
       "      <td>1864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Michael</th>\n",
       "      <td>7172</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Oscar</th>\n",
       "      <td>9656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">2020-01-02</th>\n",
       "      <th>Creed</th>\n",
       "      <td>13214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jim</th>\n",
       "      <td>8278</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Michael</th>\n",
       "      <td>6362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Oscar</th>\n",
       "      <td>8661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <th>Dwight</th>\n",
       "      <td>11912</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    Revenue\n",
       "Date       Name            \n",
       "2020-01-01 Creed       4430\n",
       "           Dwight      2639\n",
       "           Jim         1864\n",
       "           Michael     7172\n",
       "           Oscar       9656\n",
       "2020-01-02 Creed      13214\n",
       "           Jim         8278\n",
       "           Michael     6362\n",
       "           Oscar       8661\n",
       "2020-01-03 Dwight     11912"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.pivot_table(\n",
    "    index = [\"Date\", \"Name\"], values = \"Revenue\", aggfunc = \"sum\"\n",
    ").head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.3 Stacking and Unstacking Index Levels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Name</th>\n",
       "      <th>Customer</th>\n",
       "      <th>Revenue</th>\n",
       "      <th>Expenses</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>Oscar</td>\n",
       "      <td>Logistics XYZ</td>\n",
       "      <td>5250</td>\n",
       "      <td>531</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>Oscar</td>\n",
       "      <td>Money Corp.</td>\n",
       "      <td>4406</td>\n",
       "      <td>661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-01-02</td>\n",
       "      <td>Oscar</td>\n",
       "      <td>PaperMaven</td>\n",
       "      <td>8661</td>\n",
       "      <td>1401</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-01-03</td>\n",
       "      <td>Oscar</td>\n",
       "      <td>PaperGenius</td>\n",
       "      <td>7075</td>\n",
       "      <td>906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-01-04</td>\n",
       "      <td>Oscar</td>\n",
       "      <td>Paper Pound</td>\n",
       "      <td>2524</td>\n",
       "      <td>1767</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date   Name       Customer  Revenue  Expenses\n",
       "0 2020-01-01  Oscar  Logistics XYZ     5250       531\n",
       "1 2020-01-01  Oscar    Money Corp.     4406       661\n",
       "2 2020-01-02  Oscar     PaperMaven     8661      1401\n",
       "3 2020-01-03  Oscar    PaperGenius     7075       906\n",
       "4 2020-01-04  Oscar    Paper Pound     2524      1767"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Date</th>\n",
       "      <th>2020-01-01</th>\n",
       "      <th>2020-01-02</th>\n",
       "      <th>2020-01-03</th>\n",
       "      <th>2020-01-04</th>\n",
       "      <th>2020-01-05</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Creed</th>\n",
       "      <td>4430.0</td>\n",
       "      <td>13214.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3144.0</td>\n",
       "      <td>938.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dwight</th>\n",
       "      <td>2639.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11912.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7771.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Date    2020-01-01  2020-01-02  2020-01-03  2020-01-04  2020-01-05\n",
       "Name                                                              \n",
       "Creed       4430.0     13214.0         NaN      3144.0       938.0\n",
       "Dwight      2639.0         NaN     11912.0         NaN      7771.0"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "by_name_and_date = sales.pivot_table(\n",
    "    index = \"Name\", \n",
    "    columns = \"Date\", \n",
    "    values = \"Revenue\", \n",
    "    aggfunc = \"sum\"\n",
    ")\n",
    "\n",
    "by_name_and_date.head(2) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Name    Date      \n",
       "Creed   2020-01-01     4430.0\n",
       "        2020-01-02    13214.0\n",
       "        2020-01-04     3144.0\n",
       "        2020-01-05      938.0\n",
       "Dwight  2020-01-01     2639.0\n",
       "        2020-01-03    11912.0\n",
       "        2020-01-05     7771.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "by_name_and_date.stack().head(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Revenue</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Customer</th>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Average Paper Co.</th>\n",
       "      <th>Creed</th>\n",
       "      <td>13214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jim</th>\n",
       "      <td>2287</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Best Paper Co.</th>\n",
       "      <th>Dwight</th>\n",
       "      <td>2703</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Michael</th>\n",
       "      <td>15754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Logistics XYZ</th>\n",
       "      <th>Dwight</th>\n",
       "      <td>9209</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           Revenue\n",
       "Customer          Name            \n",
       "Average Paper Co. Creed      13214\n",
       "                  Jim         2287\n",
       "Best Paper Co.    Dwight      2703\n",
       "                  Michael    15754\n",
       "Logistics XYZ     Dwight      9209"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales_by_customer = sales.pivot_table(\n",
    "    index = [\"Customer\", \"Name\"],\n",
    "    values = \"Revenue\",\n",
    "    aggfunc = \"sum\"\n",
    ")\n",
    "\n",
    "sales_by_customer.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"5\" halign=\"left\">Revenue</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th>Creed</th>\n",
       "      <th>Dwight</th>\n",
       "      <th>Jim</th>\n",
       "      <th>Michael</th>\n",
       "      <th>Oscar</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Customer</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Average Paper Co.</th>\n",
       "      <td>13214.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2287.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Best Paper Co.</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2703.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>15754.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Logistics XYZ</th>\n",
       "      <td>NaN</td>\n",
       "      <td>9209.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7172.0</td>\n",
       "      <td>5250.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Money Corp.</th>\n",
       "      <td>5368.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8278.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4406.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Paper Pound</th>\n",
       "      <td>NaN</td>\n",
       "      <td>7771.0</td>\n",
       "      <td>4226.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5317.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PaperGenius</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2639.0</td>\n",
       "      <td>1864.0</td>\n",
       "      <td>12344.0</td>\n",
       "      <td>7075.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PaperMaven</th>\n",
       "      <td>3144.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3868.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8661.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   Revenue                                 \n",
       "Name                 Creed  Dwight     Jim  Michael   Oscar\n",
       "Customer                                                   \n",
       "Average Paper Co.  13214.0     NaN  2287.0      NaN     NaN\n",
       "Best Paper Co.         NaN  2703.0     NaN  15754.0     NaN\n",
       "Logistics XYZ          NaN  9209.0     NaN   7172.0  5250.0\n",
       "Money Corp.         5368.0     NaN  8278.0      NaN  4406.0\n",
       "Paper Pound            NaN  7771.0  4226.0      NaN  5317.0\n",
       "PaperGenius            NaN  2639.0  1864.0  12344.0  7075.0\n",
       "PaperMaven          3144.0     NaN  3868.0      NaN  8661.0"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales_by_customer.unstack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.4 Melting a Data Set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Name</th>\n",
       "      <th>Customer</th>\n",
       "      <th>Revenue</th>\n",
       "      <th>Expenses</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>Oscar</td>\n",
       "      <td>Logistics XYZ</td>\n",
       "      <td>5250</td>\n",
       "      <td>531</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date   Name       Customer  Revenue  Expenses\n",
       "0 2020-01-01  Oscar  Logistics XYZ     5250       531"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>NA</th>\n",
       "      <th>EU</th>\n",
       "      <th>JP</th>\n",
       "      <th>Other</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Wii Sports</td>\n",
       "      <td>41.49</td>\n",
       "      <td>29.02</td>\n",
       "      <td>3.77</td>\n",
       "      <td>8.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Super Mario Bros.</td>\n",
       "      <td>29.08</td>\n",
       "      <td>3.58</td>\n",
       "      <td>6.81</td>\n",
       "      <td>0.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Mario Kart Wii</td>\n",
       "      <td>15.85</td>\n",
       "      <td>12.88</td>\n",
       "      <td>3.79</td>\n",
       "      <td>3.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Wii Sports Resort</td>\n",
       "      <td>15.75</td>\n",
       "      <td>11.01</td>\n",
       "      <td>3.28</td>\n",
       "      <td>2.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Pokemon Red/Pokemon Blue</td>\n",
       "      <td>11.27</td>\n",
       "      <td>8.89</td>\n",
       "      <td>10.22</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       Name     NA     EU     JP  Other\n",
       "0                Wii Sports  41.49  29.02   3.77   8.46\n",
       "1         Super Mario Bros.  29.08   3.58   6.81   0.77\n",
       "2            Mario Kart Wii  15.85  12.88   3.79   3.31\n",
       "3         Wii Sports Resort  15.75  11.01   3.28   2.96\n",
       "4  Pokemon Red/Pokemon Blue  11.27   8.89  10.22   1.00"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "video_game_sales = pd.read_csv(\"video_game_sales.csv\")\n",
    "video_game_sales.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>NA</th>\n",
       "      <th>EU</th>\n",
       "      <th>JP</th>\n",
       "      <th>Other</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Wii Sports</td>\n",
       "      <td>41.49</td>\n",
       "      <td>29.02</td>\n",
       "      <td>3.77</td>\n",
       "      <td>8.46</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Name     NA     EU    JP  Other\n",
       "0  Wii Sports  41.49  29.02  3.77   8.46"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "video_game_sales.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Wii Sports</td>\n",
       "      <td>NA</td>\n",
       "      <td>41.49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Super Mario Bros.</td>\n",
       "      <td>NA</td>\n",
       "      <td>29.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Mario Kart Wii</td>\n",
       "      <td>NA</td>\n",
       "      <td>15.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Wii Sports Resort</td>\n",
       "      <td>NA</td>\n",
       "      <td>15.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Pokemon Red/Pokemon Blue</td>\n",
       "      <td>NA</td>\n",
       "      <td>11.27</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       Name variable  value\n",
       "0                Wii Sports       NA  41.49\n",
       "1         Super Mario Bros.       NA  29.08\n",
       "2            Mario Kart Wii       NA  15.85\n",
       "3         Wii Sports Resort       NA  15.75\n",
       "4  Pokemon Red/Pokemon Blue       NA  11.27"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "video_game_sales.melt(id_vars = 'Name', value_vars = \"NA\").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Wii Sports</td>\n",
       "      <td>NA</td>\n",
       "      <td>41.49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Super Mario Bros.</td>\n",
       "      <td>NA</td>\n",
       "      <td>29.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Mario Kart Wii</td>\n",
       "      <td>NA</td>\n",
       "      <td>15.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Wii Sports Resort</td>\n",
       "      <td>NA</td>\n",
       "      <td>15.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Pokemon Red/Pokemon Blue</td>\n",
       "      <td>NA</td>\n",
       "      <td>11.27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66259</th>\n",
       "      <td>Woody Woodpecker in Crazy Castle 5</td>\n",
       "      <td>Other</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66260</th>\n",
       "      <td>Men in Black II: Alien Escape</td>\n",
       "      <td>Other</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66261</th>\n",
       "      <td>SCORE International Baja 1000: The Official Game</td>\n",
       "      <td>Other</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66262</th>\n",
       "      <td>Know How 2</td>\n",
       "      <td>Other</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66263</th>\n",
       "      <td>Spirits &amp; Spells</td>\n",
       "      <td>Other</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>66264 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                   Name variable  value\n",
       "0                                            Wii Sports       NA  41.49\n",
       "1                                     Super Mario Bros.       NA  29.08\n",
       "2                                        Mario Kart Wii       NA  15.85\n",
       "3                                     Wii Sports Resort       NA  15.75\n",
       "4                              Pokemon Red/Pokemon Blue       NA  11.27\n",
       "...                                                 ...      ...    ...\n",
       "66259                Woody Woodpecker in Crazy Castle 5    Other   0.00\n",
       "66260                     Men in Black II: Alien Escape    Other   0.00\n",
       "66261  SCORE International Baja 1000: The Official Game    Other   0.00\n",
       "66262                                        Know How 2    Other   0.00\n",
       "66263                                  Spirits & Spells    Other   0.00\n",
       "\n",
       "[66264 rows x 3 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regional_sales_columns = [\"NA\", \"EU\", \"JP\", \"Other\"]\n",
    "\n",
    "video_game_sales.melt(\n",
    "    id_vars = \"Name\", value_vars = regional_sales_columns\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Region</th>\n",
       "      <th>Sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Wii Sports</td>\n",
       "      <td>NA</td>\n",
       "      <td>41.49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Super Mario Bros.</td>\n",
       "      <td>NA</td>\n",
       "      <td>29.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Mario Kart Wii</td>\n",
       "      <td>NA</td>\n",
       "      <td>15.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Wii Sports Resort</td>\n",
       "      <td>NA</td>\n",
       "      <td>15.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Pokemon Red/Pokemon Blue</td>\n",
       "      <td>NA</td>\n",
       "      <td>11.27</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       Name Region  Sales\n",
       "0                Wii Sports     NA  41.49\n",
       "1         Super Mario Bros.     NA  29.08\n",
       "2            Mario Kart Wii     NA  15.85\n",
       "3         Wii Sports Resort     NA  15.75\n",
       "4  Pokemon Red/Pokemon Blue     NA  11.27"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "video_game_sales_by_region = video_game_sales.melt(\n",
    "    id_vars = \"Name\", \n",
    "    value_vars = regional_sales_columns,\n",
    "    var_name = \"Region\",\n",
    "    value_name = \"Sales\"\n",
    ")\n",
    "\n",
    "video_game_sales_by_region.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>'98 Koshien</th>\n",
       "      <td>0.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>.hack//G.U. Vol.1//Rebirth</th>\n",
       "      <td>0.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>.hack//G.U. Vol.2//Reminisce</th>\n",
       "      <td>0.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>.hack//G.U. Vol.3//Redemption</th>\n",
       "      <td>0.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>.hack//Infection Part 1</th>\n",
       "      <td>1.26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                               Sales\n",
       "Name                                \n",
       "'98 Koshien                     0.40\n",
       ".hack//G.U. Vol.1//Rebirth      0.17\n",
       ".hack//G.U. Vol.2//Reminisce    0.23\n",
       ".hack//G.U. Vol.3//Redemption   0.17\n",
       ".hack//Infection Part 1         1.26"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "video_game_sales_by_region.pivot_table(\n",
    "    index = \"Name\", values = \"Sales\", aggfunc = \"sum\"\n",
    ").head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.5 Exploding a List of Values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Recipe</th>\n",
       "      <th>Ingredients</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Cashew Crusted Chicken</td>\n",
       "      <td>Apricot preserves, Dijon mustard, curry powder...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Tomato Basil Salmon</td>\n",
       "      <td>Salmon filets, basil, tomato, olive oil, Parme...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Parmesan Cheese Chicken</td>\n",
       "      <td>Bread crumbs, Parmesan cheese, Italian seasoni...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    Recipe                                        Ingredients\n",
       "0   Cashew Crusted Chicken  Apricot preserves, Dijon mustard, curry powder...\n",
       "1      Tomato Basil Salmon  Salmon filets, basil, tomato, olive oil, Parme...\n",
       "2  Parmesan Cheese Chicken  Bread crumbs, Parmesan cheese, Italian seasoni..."
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recipes = pd.read_csv(\"recipes.csv\")\n",
    "recipes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    [Apricot preserves,  Dijon mustard,  curry pow...\n",
       "1    [Salmon filets,  basil,  tomato,  olive oil,  ...\n",
       "2    [Bread crumbs,  Parmesan cheese,  Italian seas...\n",
       "Name: Ingredients, dtype: object"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recipes[\"Ingredients\"].str.split(\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Recipe</th>\n",
       "      <th>Ingredients</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Cashew Crusted Chicken</td>\n",
       "      <td>[Apricot preserves,  Dijon mustard,  curry pow...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Tomato Basil Salmon</td>\n",
       "      <td>[Salmon filets,  basil,  tomato,  olive oil,  ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Parmesan Cheese Chicken</td>\n",
       "      <td>[Bread crumbs,  Parmesan cheese,  Italian seas...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    Recipe                                        Ingredients\n",
       "0   Cashew Crusted Chicken  [Apricot preserves,  Dijon mustard,  curry pow...\n",
       "1      Tomato Basil Salmon  [Salmon filets,  basil,  tomato,  olive oil,  ...\n",
       "2  Parmesan Cheese Chicken  [Bread crumbs,  Parmesan cheese,  Italian seas..."
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recipes[\"Ingredients\"] = recipes[\"Ingredients\"].str.split(\",\")\n",
    "recipes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Recipe</th>\n",
       "      <th>Ingredients</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Cashew Crusted Chicken</td>\n",
       "      <td>Apricot preserves</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Cashew Crusted Chicken</td>\n",
       "      <td>Dijon mustard</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Cashew Crusted Chicken</td>\n",
       "      <td>curry powder</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Cashew Crusted Chicken</td>\n",
       "      <td>chicken breasts</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Cashew Crusted Chicken</td>\n",
       "      <td>cashews</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Tomato Basil Salmon</td>\n",
       "      <td>Salmon filets</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Tomato Basil Salmon</td>\n",
       "      <td>basil</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Tomato Basil Salmon</td>\n",
       "      <td>tomato</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Tomato Basil Salmon</td>\n",
       "      <td>olive oil</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Tomato Basil Salmon</td>\n",
       "      <td>Parmesan cheese</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Parmesan Cheese Chicken</td>\n",
       "      <td>Bread crumbs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Parmesan Cheese Chicken</td>\n",
       "      <td>Parmesan cheese</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Parmesan Cheese Chicken</td>\n",
       "      <td>Italian seasoning</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Parmesan Cheese Chicken</td>\n",
       "      <td>egg</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Parmesan Cheese Chicken</td>\n",
       "      <td>chicken breasts</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    Recipe         Ingredients\n",
       "0   Cashew Crusted Chicken   Apricot preserves\n",
       "0   Cashew Crusted Chicken       Dijon mustard\n",
       "0   Cashew Crusted Chicken        curry powder\n",
       "0   Cashew Crusted Chicken     chicken breasts\n",
       "0   Cashew Crusted Chicken             cashews\n",
       "1      Tomato Basil Salmon       Salmon filets\n",
       "1      Tomato Basil Salmon               basil\n",
       "1      Tomato Basil Salmon              tomato\n",
       "1      Tomato Basil Salmon           olive oil\n",
       "1      Tomato Basil Salmon     Parmesan cheese\n",
       "2  Parmesan Cheese Chicken        Bread crumbs\n",
       "2  Parmesan Cheese Chicken     Parmesan cheese\n",
       "2  Parmesan Cheese Chicken   Italian seasoning\n",
       "2  Parmesan Cheese Chicken                 egg\n",
       "2  Parmesan Cheese Chicken     chicken breasts"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recipes.explode(\"Ingredients\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.6 Coding Challenge"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.6.1 Problems"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Manufacturer</th>\n",
       "      <th>Year</th>\n",
       "      <th>Fuel</th>\n",
       "      <th>Transmission</th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Acura</td>\n",
       "      <td>2012</td>\n",
       "      <td>Gas</td>\n",
       "      <td>Automatic</td>\n",
       "      <td>10299</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Jaguar</td>\n",
       "      <td>2011</td>\n",
       "      <td>Gas</td>\n",
       "      <td>Automatic</td>\n",
       "      <td>9500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Honda</td>\n",
       "      <td>2004</td>\n",
       "      <td>Gas</td>\n",
       "      <td>Automatic</td>\n",
       "      <td>3995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Chevrolet</td>\n",
       "      <td>2016</td>\n",
       "      <td>Gas</td>\n",
       "      <td>Automatic</td>\n",
       "      <td>41988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Kia</td>\n",
       "      <td>2015</td>\n",
       "      <td>Gas</td>\n",
       "      <td>Automatic</td>\n",
       "      <td>12995</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Manufacturer  Year Fuel Transmission  Price\n",
       "0        Acura  2012  Gas    Automatic  10299\n",
       "1       Jaguar  2011  Gas    Automatic   9500\n",
       "2        Honda  2004  Gas    Automatic   3995\n",
       "3    Chevrolet  2016  Gas    Automatic  41988\n",
       "4          Kia  2015  Gas    Automatic  12995"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cars = pd.read_csv(\"used_cars.csv\")\n",
    "cars.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>2010</th>\n",
       "      <th>2011</th>\n",
       "      <th>2012</th>\n",
       "      <th>2013</th>\n",
       "      <th>2014</th>\n",
       "      <th>2015</th>\n",
       "      <th>2016</th>\n",
       "      <th>2017</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alabama</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Alaska</td>\n",
       "      <td>8.90</td>\n",
       "      <td>8.63</td>\n",
       "      <td>8.45</td>\n",
       "      <td>8.33</td>\n",
       "      <td>8.20</td>\n",
       "      <td>9.24</td>\n",
       "      <td>10.17</td>\n",
       "      <td>10.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>8.33</td>\n",
       "      <td>8.18</td>\n",
       "      <td>8.34</td>\n",
       "      <td>8.38</td>\n",
       "      <td>8.36</td>\n",
       "      <td>8.50</td>\n",
       "      <td>8.40</td>\n",
       "      <td>10.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Arkansas</td>\n",
       "      <td>7.18</td>\n",
       "      <td>6.96</td>\n",
       "      <td>6.82</td>\n",
       "      <td>6.72</td>\n",
       "      <td>6.61</td>\n",
       "      <td>7.92</td>\n",
       "      <td>8.35</td>\n",
       "      <td>8.68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>California</td>\n",
       "      <td>9.19</td>\n",
       "      <td>8.91</td>\n",
       "      <td>8.72</td>\n",
       "      <td>8.60</td>\n",
       "      <td>9.52</td>\n",
       "      <td>9.51</td>\n",
       "      <td>10.43</td>\n",
       "      <td>10.22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        State  2010  2011  2012  2013  2014  2015   2016   2017\n",
       "0     Alabama  0.00  0.00  0.00  0.00  0.00  0.00   0.00   0.00\n",
       "1      Alaska  8.90  8.63  8.45  8.33  8.20  9.24  10.17  10.01\n",
       "2     Arizona  8.33  8.18  8.34  8.38  8.36  8.50   8.40  10.22\n",
       "3    Arkansas  7.18  6.96  6.82  6.72  6.61  7.92   8.35   8.68\n",
       "4  California  9.19  8.91  8.72  8.60  9.52  9.51  10.43  10.22"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min_wage = pd.read_csv(\"minimum_wage.csv\")\n",
    "min_wage.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.6.2 Solutions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fuel</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Diesel</th>\n",
       "      <td>986177143</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Electric</th>\n",
       "      <td>18502957</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Gas</th>\n",
       "      <td>86203853926</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hybrid</th>\n",
       "      <td>44926064</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Other</th>\n",
       "      <td>242096286</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Price\n",
       "Fuel                 \n",
       "Diesel      986177143\n",
       "Electric     18502957\n",
       "Gas       86203853926\n",
       "Hybrid       44926064\n",
       "Other       242096286"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cars.pivot_table(\n",
    "    values = \"Price\", index = \"Fuel\", aggfunc = \"sum\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Transmission</th>\n",
       "      <th>Automatic</th>\n",
       "      <th>Manual</th>\n",
       "      <th>Other</th>\n",
       "      <th>All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Manufacturer</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Tesla</th>\n",
       "      <td>179.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>59.0</td>\n",
       "      <td>238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Toyota</th>\n",
       "      <td>31480.0</td>\n",
       "      <td>1367.0</td>\n",
       "      <td>2134.0</td>\n",
       "      <td>34981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Volkswagen</th>\n",
       "      <td>7985.0</td>\n",
       "      <td>1286.0</td>\n",
       "      <td>236.0</td>\n",
       "      <td>9507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Volvo</th>\n",
       "      <td>2665.0</td>\n",
       "      <td>155.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>2870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <td>398428.0</td>\n",
       "      <td>21005.0</td>\n",
       "      <td>21738.0</td>\n",
       "      <td>441171</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Transmission  Automatic   Manual    Other     All\n",
       "Manufacturer                                     \n",
       "Tesla             179.0      NaN     59.0     238\n",
       "Toyota          31480.0   1367.0   2134.0   34981\n",
       "Volkswagen       7985.0   1286.0    236.0    9507\n",
       "Volvo            2665.0    155.0     50.0    2870\n",
       "All            398428.0  21005.0  21738.0  441171"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cars.pivot_table(\n",
    "    values = \"Price\",\n",
    "    index = \"Manufacturer\",\n",
    "    columns = \"Transmission\",\n",
    "    aggfunc = \"count\",\n",
    "    margins = True\n",
    ").tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Transmission</th>\n",
       "      <th>Automatic</th>\n",
       "      <th>Manual</th>\n",
       "      <th>Other</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Year</th>\n",
       "      <th>Fuel</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2000</th>\n",
       "      <th>Diesel</th>\n",
       "      <td>11326.176962</td>\n",
       "      <td>14010.164021</td>\n",
       "      <td>11075.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Electric</th>\n",
       "      <td>1500.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Gas</th>\n",
       "      <td>4314.675996</td>\n",
       "      <td>6226.140327</td>\n",
       "      <td>3203.538462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hybrid</th>\n",
       "      <td>2600.000000</td>\n",
       "      <td>2400.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Other</th>\n",
       "      <td>16014.918919</td>\n",
       "      <td>11361.952381</td>\n",
       "      <td>12984.642857</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2020</th>\n",
       "      <th>Diesel</th>\n",
       "      <td>63272.595930</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1234.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Electric</th>\n",
       "      <td>8015.166667</td>\n",
       "      <td>2200.000000</td>\n",
       "      <td>20247.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Gas</th>\n",
       "      <td>34925.857933</td>\n",
       "      <td>36007.270833</td>\n",
       "      <td>20971.045455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hybrid</th>\n",
       "      <td>35753.200000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1234.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Other</th>\n",
       "      <td>22210.306452</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2725.925926</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>102 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Transmission      Automatic        Manual         Other\n",
       "Year Fuel                                              \n",
       "2000 Diesel    11326.176962  14010.164021  11075.000000\n",
       "     Electric   1500.000000           NaN           NaN\n",
       "     Gas        4314.675996   6226.140327   3203.538462\n",
       "     Hybrid     2600.000000   2400.000000           NaN\n",
       "     Other     16014.918919  11361.952381  12984.642857\n",
       "...                     ...           ...           ...\n",
       "2020 Diesel    63272.595930      1.000000   1234.000000\n",
       "     Electric   8015.166667   2200.000000  20247.500000\n",
       "     Gas       34925.857933  36007.270833  20971.045455\n",
       "     Hybrid    35753.200000           NaN   1234.000000\n",
       "     Other     22210.306452           NaN   2725.925926\n",
       "\n",
       "[102 rows x 3 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cars.pivot_table(\n",
    "    values = \"Price\",\n",
    "    index = [\"Year\", \"Fuel\"],\n",
    "    columns = [\"Transmission\"],\n",
    "    aggfunc = \"mean\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "report = cars.pivot_table(\n",
    "    values = \"Price\",\n",
    "    index = [\"Year\", \"Fuel\"],\n",
    "    columns = [\"Transmission\"],\n",
    "    aggfunc = \"mean\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Year  Fuel      Transmission\n",
       "2000  Diesel    Automatic       11326.176962\n",
       "                Manual          14010.164021\n",
       "                Other           11075.000000\n",
       "      Electric  Automatic        1500.000000\n",
       "      Gas       Automatic        4314.675996\n",
       "                                    ...     \n",
       "2020  Gas       Other           20971.045455\n",
       "      Hybrid    Automatic       35753.200000\n",
       "                Other            1234.000000\n",
       "      Other     Automatic       22210.306452\n",
       "                Other            2725.925926\n",
       "Length: 274, dtype: float64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "report.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alabama</td>\n",
       "      <td>2010</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Alaska</td>\n",
       "      <td>2010</td>\n",
       "      <td>8.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>2010</td>\n",
       "      <td>8.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Arkansas</td>\n",
       "      <td>2010</td>\n",
       "      <td>7.18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>California</td>\n",
       "      <td>2010</td>\n",
       "      <td>9.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>435</th>\n",
       "      <td>Virginia</td>\n",
       "      <td>2017</td>\n",
       "      <td>7.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>436</th>\n",
       "      <td>Washington</td>\n",
       "      <td>2017</td>\n",
       "      <td>11.24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>437</th>\n",
       "      <td>West Virginia</td>\n",
       "      <td>2017</td>\n",
       "      <td>8.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>438</th>\n",
       "      <td>Wisconsin</td>\n",
       "      <td>2017</td>\n",
       "      <td>7.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>439</th>\n",
       "      <td>Wyoming</td>\n",
       "      <td>2017</td>\n",
       "      <td>5.26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>440 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             State variable  value\n",
       "0          Alabama     2010   0.00\n",
       "1           Alaska     2010   8.90\n",
       "2          Arizona     2010   8.33\n",
       "3         Arkansas     2010   7.18\n",
       "4       California     2010   9.19\n",
       "..             ...      ...    ...\n",
       "435       Virginia     2017   7.41\n",
       "436     Washington     2017  11.24\n",
       "437  West Virginia     2017   8.94\n",
       "438      Wisconsin     2017   7.41\n",
       "439        Wyoming     2017   5.26\n",
       "\n",
       "[440 rows x 3 columns]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "year_columns = [\n",
    "    \"2010\", \"2011\", \"2012\", \"2013\", \n",
    "    \"2014\", \"2015\", \"2016\", \"2017\"\n",
    "]\n",
    "\n",
    "min_wage.melt(id_vars = \"State\", value_vars = year_columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alabama</td>\n",
       "      <td>2010</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Alaska</td>\n",
       "      <td>2010</td>\n",
       "      <td>8.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>2010</td>\n",
       "      <td>8.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Arkansas</td>\n",
       "      <td>2010</td>\n",
       "      <td>7.18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>California</td>\n",
       "      <td>2010</td>\n",
       "      <td>9.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>435</th>\n",
       "      <td>Virginia</td>\n",
       "      <td>2017</td>\n",
       "      <td>7.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>436</th>\n",
       "      <td>Washington</td>\n",
       "      <td>2017</td>\n",
       "      <td>11.24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>437</th>\n",
       "      <td>West Virginia</td>\n",
       "      <td>2017</td>\n",
       "      <td>8.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>438</th>\n",
       "      <td>Wisconsin</td>\n",
       "      <td>2017</td>\n",
       "      <td>7.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>439</th>\n",
       "      <td>Wyoming</td>\n",
       "      <td>2017</td>\n",
       "      <td>5.26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>440 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             State variable  value\n",
       "0          Alabama     2010   0.00\n",
       "1           Alaska     2010   8.90\n",
       "2          Arizona     2010   8.33\n",
       "3         Arkansas     2010   7.18\n",
       "4       California     2010   9.19\n",
       "..             ...      ...    ...\n",
       "435       Virginia     2017   7.41\n",
       "436     Washington     2017  11.24\n",
       "437  West Virginia     2017   8.94\n",
       "438      Wisconsin     2017   7.41\n",
       "439        Wyoming     2017   5.26\n",
       "\n",
       "[440 rows x 3 columns]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min_wage.melt(id_vars = \"State\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Year</th>\n",
       "      <th>Wage</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alabama</td>\n",
       "      <td>2010</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Alaska</td>\n",
       "      <td>2010</td>\n",
       "      <td>8.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>2010</td>\n",
       "      <td>8.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Arkansas</td>\n",
       "      <td>2010</td>\n",
       "      <td>7.18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>California</td>\n",
       "      <td>2010</td>\n",
       "      <td>9.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>435</th>\n",
       "      <td>Virginia</td>\n",
       "      <td>2017</td>\n",
       "      <td>7.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>436</th>\n",
       "      <td>Washington</td>\n",
       "      <td>2017</td>\n",
       "      <td>11.24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>437</th>\n",
       "      <td>West Virginia</td>\n",
       "      <td>2017</td>\n",
       "      <td>8.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>438</th>\n",
       "      <td>Wisconsin</td>\n",
       "      <td>2017</td>\n",
       "      <td>7.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>439</th>\n",
       "      <td>Wyoming</td>\n",
       "      <td>2017</td>\n",
       "      <td>5.26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>440 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             State  Year   Wage\n",
       "0          Alabama  2010   0.00\n",
       "1           Alaska  2010   8.90\n",
       "2          Arizona  2010   8.33\n",
       "3         Arkansas  2010   7.18\n",
       "4       California  2010   9.19\n",
       "..             ...   ...    ...\n",
       "435       Virginia  2017   7.41\n",
       "436     Washington  2017  11.24\n",
       "437  West Virginia  2017   8.94\n",
       "438      Wisconsin  2017   7.41\n",
       "439        Wyoming  2017   5.26\n",
       "\n",
       "[440 rows x 3 columns]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min_wage.melt(\n",
    "    id_vars = \"State\", var_name = \"Year\", value_name = \"Wage\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.7 Summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
